class NewThread implements Runnable {
  Thread t;
  NewThread() {
    t = new Thread(this, "Demo Thread");
    System.out.println("child thread: " + t); 
    t.start();
  }

  // This is the entry point for the second thread.
  public void run(){
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println("Child Thread: " + i); 
        // Let the thread sleep for a while.
        Thread.sleep(500);
      }   
    }catch (InterruptedException e) {
      System.out.println("Child Interrupted.");
    }   
    System.out.print("Exiting child thread.");
 } 
}

public class ThreadDemo { 
  public static void main(String args[]) {
    new NewThread(); // create a new thread
    try {
      for (int i = 5; i > 0; i-- ) { 
        System.out.println("Main thread: " + i); 
        Thread.sleep(1000);
      }
    }catch (InterruptedException e) {
        System.out.print("Main thread interrupted.");
      }   
    }   
}
